package com.cndym.utils;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * User: mcs
 * Date: 12-3-16
 * Time: 上午10:31
 */
public class SmsTemplateUtils {

    private static Map<String, String> map = new LinkedHashMap<String, String>();

    private static Map<String,String> codeNameMap = new LinkedHashMap<String, String>();

    static {
        forInstance();
    }

    private static void forInstance() {

        try {
            SAXReader xmlReader = new SAXReader();
            Document document = xmlReader.read(Utils.getClassPath() + ConfigUtils.getValue("SMS_TEMPLATE_PATH"));
            List list = document.selectNodes("/templates/template");
            Iterator iterator = list.iterator();
            while (iterator.hasNext()) {
                Element element = (Element) iterator.next();
                map.put(element.attributeValue("code"), element.getTextTrim());
                codeNameMap.put(element.attributeValue("code"),element.attributeValue("name"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Map<String, String> getSmsTemplateMap() {
        return codeNameMap;
    }

    public static String getTemplateText(String code) {
        if (Utils.isNotEmpty(map)) {
            return map.get(code);
        }
        return "";
    }
}
